package com.kxf.androidtestdemo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.kxf.androidtestdemo.R;
import com.kxf.androidtestdemo.bean.JsTest;

public class JsTestActivity extends AppCompatActivity {
    private WebView wv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_js_test);

        wv = findViewById(R.id.wv);
        WebSettings setting = wv.getSettings();
        setting.setJavaScriptEnabled(true);
        setting.setDomStorageEnabled(true);
        wv.addJavascriptInterface(new JsTest(), "androidjs");
        wv.loadUrl("file:///android_asset/index.html");

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String str1 = "javascript:callback("+"'hello kxf'"+")";
                        Log.d("JsTestActivity", "str1   " + str1);
                        wv.loadUrl(str1);
                    }
                });
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String str = String.format("javascript:onResult(\'%s\')", "hello kxf111");
                        Log.d("JsTestActivity", "str   " + str);
                        wv.loadUrl(str);
                    }
                });
            }
        }).start();
    }
    /**
     *
     function a(){
     String str = "aaa";
     alert(str);
     }
     */
}
